//Start functions of Package
var PackageTemplate = "templates/Package.xsl"

function loadPackageData(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.package?id=' + xID + '&m_id=' + modelID , true)
		objectHTTPXML.onreadystatechange = returnPackageData
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnPackageData(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(PackageTemplate, xml)
		document.getElementById("divBody").innerHTML = template
		waitBox.hideBox(null);
	}			
}

function savePackage(xID){
	if(objectHTTPXML) {
		var parameters = "m_id=" + modelID +
						 "&id=" + encodeURI(document.getElementById("id").value) +
						 "&name=" + encodeURI(document.getElementById("name").value) +
						 "&uri=" + encodeURI(document.getElementById("uri").value)
						 
		xIDTmp = xID
		
		waitBox = new Lightbox.base('box1');

		objectHTTPXML.onreadystatechange = returnSavePackage
		objectHTTPXML.open('POST', '/model.save.package', true);
		objectHTTPXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objectHTTPXML.setRequestHeader("Content-length", parameters.length);
		objectHTTPXML.setRequestHeader("Connection", "close");
		objectHTTPXML.send(parameters);
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnSavePackage(){
	if (objectHTTPXML.readyState == 4) {
		waitBox.hideBox(null);
		if(objectHTTPXML.responseText.split("$$$$").length != 0){
			changeName(xIDTmp, document.getElementById("name").value)
			alert('Data saved!')
		} else {
			alert('Data not saved!')
			loadPackageData(xIDTmp)
		}
	}			
}

function createPackage(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.create.package?&m_id=' + modelID + '&p_id=' + xID , true)
		objectHTTPXML.onreadystatechange = returnCreatePackage
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnCreatePackage(){
	if (objectHTTPXML.readyState == 4) {
		if(objectHTTPXML.responseText != 'ERROR'){
			id = objectHTTPXML.responseText.split("$$$$")
			createNewNode(xIDTmp, id[0], id[1], 'javascript:loadPackageData("' + id[0] + '");', 'packageopen.gif', 'packageclose.gif')
		} else {
			alert('Error to create the package!')
		}
		waitBox.hideBox(null);
	}			
}

//End functions Package